package com.supms.course.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.CourseTargetMapper;
import com.supms.course.domain.CourseTarget;
import com.supms.course.service.ICourseTargetService;

/**
 * 课程目标达成度Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-11
 */
@Service
public class CourseTargetServiceImpl implements ICourseTargetService 
{
    @Autowired
    private CourseTargetMapper courseTargetMapper;

    /**
     * 查询课程目标达成度
     * 
     * @param id 课程目标达成度主键
     * @return 课程目标达成度
     */
    @Override
    public CourseTarget selectCourseTargetById(Long id)
    {
        return courseTargetMapper.selectCourseTargetById(id);
    }

    /**
     * 查询课程目标达成度列表
     * 
     * @param courseTarget 课程目标达成度
     * @return 课程目标达成度
     */
    @Override
    public List<CourseTarget> selectCourseTargetList(CourseTarget courseTarget)
    {
        String operName = SecurityUtils.getUsername();
        courseTarget.setCreateBy(operName);
        return courseTargetMapper.selectCourseTargetList(courseTarget);
    }

    /**
     * 新增课程目标达成度
     * 
     * @param courseTarget 课程目标达成度
     * @return 结果
     */
    @Override
    public int insertCourseTarget(CourseTarget courseTarget)
    {
        String operName = SecurityUtils.getUsername();
        courseTarget.setCreateBy(operName);
        courseTarget.setSupName(courseTargetMapper.getSupNameByOperName(operName));
        courseTarget.setCreateTime(DateUtils.getNowDate());
        return courseTargetMapper.insertCourseTarget(courseTarget);
    }

    /**
     * 修改课程目标达成度
     * 
     * @param courseTarget 课程目标达成度
     * @return 结果
     */
    @Override
    public int updateCourseTarget(CourseTarget courseTarget)
    {
        String operName = SecurityUtils.getUsername();
        courseTarget.setUpdateBy(operName);
        courseTarget.setUpdateTime(DateUtils.getNowDate());
        return courseTargetMapper.updateCourseTarget(courseTarget);
    }

    /**
     * 批量删除课程目标达成度
     * 
     * @param ids 需要删除的课程目标达成度主键
     * @return 结果
     */
    @Override
    public int deleteCourseTargetByIds(Long[] ids)
    {
        // 判断课程目标达成度检查表中是否做了评价，有则先删除评价表
        Long[] haveIds = courseTargetMapper.selectCourseTargetEvalFlagByIds(ids);
        if (haveIds.length > 0) {
            courseTargetMapper.deleteCourseTargetEvalByIds(haveIds);
        }
        return courseTargetMapper.deleteCourseTargetByIds(ids);
    }

    /**
     * 删除课程目标达成度信息
     * 
     * @param id 课程目标达成度主键
     * @return 结果
     */
    @Override
    public int deleteCourseTargetById(Long id)
    {
        return courseTargetMapper.deleteCourseTargetById(id);
    }

    /**
     * 获取本科课程目标达成度检查表（数据综合查看表）
     * @param courseTarget 接收到要查看表的对象
     * @return 表内容及评价结果对象
     */
    @Override
    public CourseTarget getCourseTargetTable(CourseTarget courseTarget) {
        // 判断是否已经评价，没有评价的话不查询评价结果
        String courseTargetEvalFlag = courseTarget.getCourseTargetEvalFlag();
        // "0" 为未评价，"1" 为已评价
        if ("0".equals(courseTargetEvalFlag)) {
            return courseTargetMapper.getCourseTargetTable0(courseTarget);
        } else if ("1".equals(courseTargetEvalFlag)) {
            return courseTargetMapper.getCourseTargetTable1(courseTarget);
        } else return null;
    }
}
